package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.MedicineSetting;
import com.itheima.service.MedicineSettingService;
import com.itheima.utils.POIUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author ZhaoShengChao
 * @create 2021/8/7 21:12
 */
@RequestMapping("/medicineSetting")
@RestController
public class MedicineSettingController {
    
    @Reference
    private MedicineSettingService medicineSettingService;

    /**
     * 文件上传
     * @param excelFile
     * @return
     */

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> excelList = POIUtils.readExcel(excelFile);

            List<MedicineSetting> medicineSettings = new ArrayList<>();
            //循环遍历excelList 得到每一行的数据

            for (String[] strings : excelList) {
                String mdCode = strings[0];
                String mdName = strings[1];
                String mdFrequency = strings[2];
                String eatQuality = strings[3];


                medicineSettings.add(new MedicineSetting(mdCode, mdName,mdFrequency,eatQuality));
            }
            medicineSettingService.add(medicineSettings);
        } catch (IOException e) {

            return new Result(false, MessageConstant.IMPORT_MEDICINESETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_MEDICINESETTING_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return medicineSettingService.pageQuery(queryPageBean);
    }

    /**
     * 添加数据
     * @param medicineSetting
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody MedicineSetting medicineSetting){
        System.out.println(medicineSetting);
        try {
            medicineSettingService.add(medicineSetting);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_MEDICINESETTING_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MEDICINESETTING_SUCCESS);
    }

    /**
     * 编辑
     * @param medicineSetting
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody MedicineSetting medicineSetting){
        //调用方法
        try {
            medicineSettingService.edit(medicineSetting);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEDICINESETTING_FAIL);

        }
        return new Result(true,MessageConstant.EDIT_MEDICINESETTING_SUCCESS);
    }

    /**
     *编辑操作
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        //调用方法
        try {
            medicineSettingService.deleteById(id);
        }catch(Exception e){
            e.printStackTrace();

            return new Result(false,MessageConstant.DELETE_MEDICINESETTING_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEDICINESETTING_SUCCESS);
    }
}
